package study.itheima.service;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import study.itheima.dao.UserMap;
import study.itheima.domain.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
//业务层
public class UserService {
    public List<User> findAllService(Integer i){
        List<User> list = null;
        try {
            InputStream is = Resources.getResourceAsStream("coreconfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();
            UserMap user = sqlSession.getMapper(UserMap.class);
            PageHelper.startPage(i,2);
            list = user.findAll();
            System.out.println(list);
            sqlSession.commit();
            sqlSession.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public User findBynameageService(String name,int age){
        User use = null;
        try {
            InputStream is = Resources.getResourceAsStream("coreconfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = build.openSession();
            UserMap user = sqlSession.getMapper(UserMap.class);
            use = user.findBynameage(name, age);
            sqlSession.commit();
            sqlSession.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return use;
    }
}
